package neusoft.edu.cn.ui_demo.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import neusoft.edu.cn.ui_demo.MainActivity;
import neusoft.edu.cn.ui_demo.R;
import neusoft.edu.cn.ui_demo.beans.Shop;
import neusoft.edu.cn.ui_demo.retrofitservice.RetrofitService;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class ShopFragment extends Fragment{

    private ImageView imageView;
    private TextView shopNameView;
    private TextView shopAddress;
    private TextView shopPhone;

    public ShopFragment() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_shop, container, false);

    }

    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        imageView = view.findViewById(R.id.imageView);
        shopNameView = view.findViewById(R.id.shopNameView);
        shopAddress = view.findViewById(R.id.shopAddress);
        shopPhone = view.findViewById(R.id.shopPhone);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .build();

        RetrofitService retrofitService = retrofit.create(RetrofitService.class);

        Call<ResponseBody> shopCall = retrofitService.getShopById("1");

        shopCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                try {

                    byte[] bytes = response.body().bytes();
                    String text = new String(bytes);

                    Gson gson = new Gson();
                    Shop shop = gson.fromJson(text,Shop.class);

                    String shopName = shop.getShopname();
                    String pic = shop.getPic();

                    shopNameView.setText(shopName);
                    shopAddress.setText(shop.getAddress());
                    shopPhone.setText(shop.getPhonenum());

                    Picasso.get()
                            .load("http://172.24.10.175:8080/foodService"+pic)
                            .into(imageView);

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(getContext(), t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        });


    }
}
